<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *空调管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'配置方案列表'),
);
$onepluginname='jy_kongtiaoshop';
//空调分类
if($_RQ['op']=='list'){
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['id']){
		$params['homeid']=$_RQ['id'];
		$return['params']['id']=$_RQ['id'];
		$_SESSION['nowid']=$_RQ['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$tip='[温馨提示]：不同的户型有不同的配置方案！';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_kongtiaoshop_peilist',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['pcd']=json_decode($item['pcd'],true);
			$item['shequtitle']=pdo_getcolumn('jy_kongtiaoshop_shequ',array('id'=>$item['shequid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['hometitle']=pdo_getcolumn('jy_kongtiaoshop_home',array('id'=>$item['homeid'],'uniacid'=>$_SESSION['uniacid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'配置方案列表'),
			array('op'=>'edit','title'=>'添加配置方案'),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'方案名称','type'=>'string'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'hometypeid','title'=>'建筑类型','type'=>'select','options'=>'hometypes'),
			array('name'=>'shequtitle','title'=>'所属小区','type'=>'string'),
			array('name'=>'hometitle','title'=>'户型','type'=>'string'),
			array('name'=>'mianji','title'=>'房间总面积','type'=>'unitstring','unit'=>'m²'),
			array('name'=>'peibi','title'=>'配比','type'=>'unitstring','unit'=>'倍'),
			array('name'=>'onewfee','title'=>'单位面积负荷','type'=>'unitstring','unit'=>'W/m²'),
			array('name'=>'wfee','title'=>'总冷负荷','type'=>'unitstring','unit'=>'W'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search_one'=>array('name'=>'title','title'=>'方案名称','add'=>false),
	);
	$return['status']=array('未启用','已启用');
	$return['stores']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['hometypes']=pdo_getall('jy_kongtiaoshop_home_type',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	$select=intval($_RQ['select']);
	if($id){
		$item=pdo_get('jy_kongtiaoshop_peilist',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$home=pdo_get('jy_kongtiaoshop_home',array('id'=>$item['homeid'],'uniacid'=>$_SESSION['uniacid']));
		$item['storeid']=$home['storeid'];
		$item['shequid']=$home['shequid'];
		$item['hometypeid']=$home['hometypeid'];
		$item['mianji']=$home['mianji'];
		$item['peibi']=$home['peibi'];
		$item['onewfee']=$home['onewfee'];
		$item['wfee']=$home['wfee'];
		$item['open']=$home['open'];
		$item['peiinfo']=json_decode($item['peiinfo'],true);
	}elseif($select){
		$item=array(
			'select'=>1,
			'shequid'=>0,
		);
	}else{
		$home=pdo_get('jy_kongtiaoshop_home',array('id'=>$_SESSION['nowid'],'uniacid'=>$_SESSION['uniacid']));
		$item=array(
			'homeid'=>$_SESSION['nowid'],
			'storeid'=>$home['storeid'],
			'shequid'=>$home['shequid'],
			'hometypeid'=>$home['hometypeid'],
			'mianji'=>intval($home['mianji']),
			'peibi'=>trim($home['peibi']),
			'onewfee'=>intval($home['onewfee']),
			'wfee'=>intval($home['wfee']),
			'open'=>trim($home['open']),
		);
		$item['peiinfo']['goods']=json_decode($home['houseinfo'],true);
	}
	$item['peiinfo']=$this->model('model')->uppeiinfo($item['peiinfo']);
	$item['goods']=$item['peiinfo']['goods'];
	$item['kongjian']=$item['peiinfo']['kongjian'];
	$item['waijiid']=$item['peiinfo']['waijiid'];
	$item['home']=$home;
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
			array('name'=>'home.title','title'=>'户型名称','type'=>'copy'),
			array('name'=>'hometypeid','title'=>'建筑类型','type'=>'select','options'=>'hometypes','read'=>true),
			array('name'=>'home.mianji','title'=>'房间总面积','type'=>'copy'),
			array('name'=>'title','title'=>'方案名称','type'=>'string'),
			array('name'=>'type','title'=>'方案类型','type'=>'radio','options'=>'types'),
		)
	);
	$htmls['edit'][]=array(
		'type'=>'array',
		'show'=>array('type'=>0),
		'noadd'=>1,
		'nodel'=>1,
		'name'=>'goods',
		'title'=>'中央内机',
		'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'房间名称'),
			array('name'=>'mianji','title'=>'房间面积','type'=>'unitstring','unit'=>'m²'),
			array('type'=>'select','name'=>'goodsid','title'=>'内机','options'=>'goods1')
		),
		'details'=>array(
			array('type'=>'string','name'=>'title','title'=>'房间名称','read'=>true),
			array('name'=>'mianji','title'=>'房间面积','type'=>'unitstring','unit'=>'m²','read'=>true),	
			array('type'=>'select','name'=>'goodsid','title'=>'内机','options'=>'goods1')			
		)
	);
	$htmls['edit'][]=array(
		'type'=>'array',
		'show'=>array('type'=>1),
		'noadd'=>1,
		'nodel'=>1,
		'name'=>'goods',
		'title'=>'普通空调',
		'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'房间名称'),
			array('name'=>'mianji','title'=>'房间面积','type'=>'unitstring','unit'=>'m²'),
			array('type'=>'select','name'=>'goodsid','title'=>'空调','options'=>'goods0')
		),
		'details'=>array(
			array('type'=>'string','name'=>'title','title'=>'房间名称','read'=>true),
			array('name'=>'mianji','title'=>'房间面积','type'=>'unitstring','unit'=>'m²','read'=>true),	
			array('type'=>'select','name'=>'goodsid','title'=>'空调','options'=>'goods0')			
		)
	);
	$htmls['edit'][]=array('type'=>'select','name'=>'waijiid','title'=>'中央外机'.$item['wfee'].'W','options'=>'goods3','show'=>array('type'=>0));
	$htmls['edit'][]=array(
		'type'=>'array',
		'show'=>array('type'=>0),
		'name'=>'kongjian',
		'title'=>'线控器',
		'add_tip'=>'添加一种线控器',
		'list'=>array(
			array('type'=>'select','name'=>'goodsid','title'=>'选择线控器','options'=>'goods2'),
			array('name'=>'total','title'=>'数量','type'=>'unitstring','unit'=>'个'),			
		),
		'details'=>array(
			array('type'=>'select','name'=>'goodsid','title'=>'选择线控器','options'=>'goods2'),
			array('name'=>'total','title'=>'数量','type'=>'unitstring','unit'=>'个'),	
		)
	);
	$htmls['edit'][]=array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status');
	$return['types']=array('中央空调','普通家用空调');
	$return['status']=array('未启用','已启用');
	$return['goods1']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'onepluginname'=>'jy_kongtiaoshop','type <>'=>4,'nochecked'=>0,'no_showsys'=>0,'no_onebuy'=>1),array('id','title','price','api_data'),'id');
	foreach($return['goods1'] as &$gv1){
		$gv1['api_data']=json_decode($gv1['api_data'],true);
		$gv1['title']=$gv1['title'].'('.$gv1['api_data']['sn'].')'.'('.$gv1['api_data']['wfee'].'W)'.'(￥'.$gv1['price'].')';
	}
	$return['goods0']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'onepluginname'=>'jy_kongtiaoshop','type <>'=>4,'nochecked'=>0,'no_showsys'=>0,'no_onebuy'=>0),array('id','title','price','api_data'),'id');
	foreach($return['goods0'] as &$gv1){
		$gv1['api_data']=json_decode($gv1['api_data'],true);
		$gv1['title']=$gv1['title'].'('.$gv1['api_data']['sn'].')'.'('.$gv1['api_data']['wfee'].'W)'.'(￥'.$gv1['price'].')';
	}
	$return['goods2']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'onepluginname'=>'jy_kongtiaoshop','type <>'=>4,'nochecked'=>0,'no_showsys'=>0,'no_onebuy'=>2),array('id','title','price','api_data'),'id');
	foreach($return['goods2'] as &$gv1){
		$gv1['api_data']=json_decode($gv1['api_data'],true);
		$gv1['title']=$gv1['title'].'('.$gv1['api_data']['sn'].')'.'(￥'.$gv1['price'].')';
	}
	$return['goods3']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'onepluginname'=>'jy_kongtiaoshop','type <>'=>4,'nochecked'=>0,'no_showsys'=>0,'no_onebuy'=>3),array('id','title','price','api_data'),'id');
	foreach($return['goods3'] as &$gv1){
		$gv1['api_data']=json_decode($gv1['api_data'],true);
		$gv1['title']=$gv1['title'].'('.$gv1['api_data']['sn'].')'.'('.$gv1['api_data']['wfee'].'W)'.'(￥'.$gv1['price'].')';
	}
	$return['hometypes']=pdo_getall('jy_kongtiaoshop_home_type',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('标题不能为空','error');
	}
	$postdata['peiinfo']['goods']=$postdata['goods'];
	$postdata['peiinfo']['kongjian']=$postdata['kongjian'];
	$postdata['peiinfo']['waijiid']=$postdata['waijiid'];
	$data=array(
		'title'=>trim($postdata['title']),
		'peibi'=>trim($postdata['peibi']),
		'onewfee'=>intval($postdata['onewfee']),
		'open'=>intval($postdata['open']),
		'status'=>intval($postdata['status']),
		'housenum'=>intval($postdata['housenum']),
		'tingnum'=>intval($postdata['tingnum']),
		'type'=>intval($postdata['type']),
		'peiinfo'=>ijson_encode($postdata['peiinfo']),
		'hometypeid'=>$postdata['hometypeid'],
		'storeid'=>$postdata['storeid'],
		'shequid'=>$postdata['shequid'],
		'mianji'=>$postdata['mianji'],
		'wfee'=>intval($postdata['wfee']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_kongtiaoshop_peilist',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		$data['homeid']=$postdata['homeid'];
		$data['createtime']=time();
		pdo_insert('jy_kongtiaoshop_peilist',$data);
		$insertid=pdo_insertid();
	}
	exi('操作成功！','','goback');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_kongtiaoshop_peilist',array('id'=>$id,'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}